package com.sskj.functions.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.IncomeDetailPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.IncomeDetailBean;
import com.sskj.lib.util.CommonUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/07/16
 */
@Route(path = RConfig.FUNCTION_INCOME_DETAIL)
public class IncomeDetailActivity extends BaseActivity<IncomeDetailPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Autowired
    String order_id;

    private BaseAdapter<IncomeDetailBean> adapter;

    private int page = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_income_detail;
    }

    @Override
    public IncomeDetailPresenter getPresenter() {
        return new IncomeDetailPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.shouyimingxi));
        ARouter.getInstance().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<IncomeDetailBean>(R.layout.functions_item_income_detail, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, IncomeDetailBean item) {
                holder.setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_income, CommonUtil.dealCoinNum(item.getPrice(), item.getType()) + " " + item.getType())
                        .setTextColor(R.id.tv_status, "1".equals(item.getStatus()) ? ContextCompat.getColor(App.INSTANCE, R.color.lib_theme) : ContextCompat.getColor(App.INSTANCE, R.color.lib_969))
                        .setText(R.id.tv_status, "1".equals(item.getStatus()) ? App.INSTANCE.getString(R.string.yijiexi) : App.INSTANCE.getString(R.string.weijiexi));
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getIncomeList(true, order_id, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getIncomeList(false, order_id, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getIncomeList(false, order_id, page, size);
        });
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }

    public void getIncomeListOK(List<IncomeDetailBean> list) {
        if (list == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(list);
        } else {
            adapter.addData(list);
        }
    }
}
